package com.changgou.seckill.web.controller;

import com.changgou.entity.Result;
import com.changgou.seckill.feign.SecKillGoodsFeign;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author LQQ
 * @Date 2020/3/18 11:14
 * @Version 1.0
 */
@Controller
@RequestMapping("/wseckillgoods")//类路径需要和网关跳转的路径一致
public class SecKillGoodsController {
    @Autowired
    private SecKillGoodsFeign secKillGoodsFeign;

    /**
     * 跳转秒杀首页
     *
     * @return
     */
    @RequestMapping("/toIndex")
    public String toIndex() {

        return "seckill-index";
    }


    /**
     * 获取秒杀时间段集合信息
     *
     * @return
     */
    @RequestMapping("/timeMenus")
    @ResponseBody//返回的是集合,得加这个注解
    public List<String> dateMenus() {
        //获取当前时间段相关的集合信息
        List<Date> dateMenus = DateUtil.getDateMenus();

        //返回值集合
        List<String> result = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (Date dateMenu : dateMenus) {
            String format = simpleDateFormat.format(dateMenu);

            result.add(format);

        }

        return result;

    }

    /**
     * 根据秒杀时间段查询Redis中的商品信息
     * @param time
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result<List<SeckillGoods>> list(String time) {

        String s = DateUtil.formatStr(time);

        Result<List<SeckillGoods>> list = secKillGoodsFeign.list(s);

        return list;

    }

}
